<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: ReportController.php 245 2011-06-27 03:31:19Z beimuaihui $
 */


class Mall_ReportController extends Baogg_Controller_Action {

	function init() 
	{		
		Baogg_Language::loadFile("user");
	}
	
	function userAction(){
		
		$params=$this->getRequest()->getParams();
		$period=5;
		$arr_year=array();
		$cur_year=(int)date("Y");
		$period=5;
		for($i=$cur_year;$i>$cur_year-$period;$i--){
			$arr_year[]=$i;
		}

		if(isset($params['user_id'])){
			$url_param['user_id']=$params['user_id'];
		}else{
			$url_param['user_id']=$_SESSION['admin']['user']['user_id'];
		}
		if(isset($params['begin_time'])){
			$url_param['begin_time']=(int)$params['begin_time'];
		}else{
			$url_param['begin_time']=(int)date("Y");
		}
		if(isset($params['model_id'])){
			$url_param['model_id']=$params['model_id'];
		}
		
		

		$arr_url=array("module"=>"mall","controller"=>"report","action"=>$params['action']."content");
		$arr_url=array_merge($arr_url,$url_param);
		$arr_user_url=array("module"=>"system","controller"=>"user","action"=>"store");
		
		$cur_year=(int)date("Y");
		$arr_year=array();
		for($i=$cur_year;$i>$cur_year-$period;$i--){
			$arr_year[]=$i;
		}
		
		$this->view->url=Baogg_Controller_Url::generate($arr_url);
		$this->view->user_url=Baogg_Controller_Url::generate($arr_user_url);
		
		$this->view->url=Baogg_Controller_Url::generate($arr_url);

		$this->view->user_url=Baogg_Controller_Url::generate($arr_user_url);
		//echo '<pre>';print_r($arr_user_url);var_dump(Baogg_Controller_Url::generate($arr_url));exit;
		$this->view->user_id=$url_param['user_id'];
		
		$this->view->begin_time_data=Zend_Json::encode($arr_year);
		$this->view->begin_time=$url_param['begin_time'];
		$this->view->arr_year=Zend_Json::encode($arr_year);
		$this->view->action=$params['action']."content";
		
	}

	function usercontentAction(){
		$params=$this->getRequest()->getParams();

		

		if(isset($params['user_id'])){
			$user_id=$params['user_id'];
		}else{
			$user_id=$_SESSION['admin']['user']['user_id'];
		}
		if(isset($params['begin_time'])){
			$begin_time=$params['begin_time'];
		}else{
			$begin_time=date("Y");
		}

		$User=new User();
		$rs_user=$User->getOne($user_id);		
		$this->view->user=@$rs_user[0];
		
		$Model=new Model();
		$arr_model=array();
		$dir=BAOGG_UPLOAD_DIR."rule/";
		$rs_model=$Model->getList(array("state"=>1,"main_table"=>"user"));
		$total_score=0;
		foreach((array)$rs_model as $schema){
			$arr_model[$schema['ext_table']]=array("name"=>$schema['model_name'],"score"=>0,"demo"=>"","items"=>array());
			$ModelTable=new ModelTable((int)$schema['model_id']);
			$rs_model_table=$ModelTable->getShowList(array("user_id"=>(int)$user_id,"begin_time"=>$begin_time."-%"));
			foreach((array)$rs_model_table as $k=>$v){
				$file=$dir.$schema['rule_id'].".php";
				$v['score']=include $file;
				$total_score+=$v['score'];

				$ModelTable->_update(array("score"=>$v['score']),$v['id']);

				$arr_model[$schema['ext_table']]['score']+=$v['score'];
				$arr_model[$schema['ext_table']]['demo'] .= @$v['demo'].";";
				$arr_model[$schema['ext_table']]['items'][]=$v;			
			}
		}
		foreach((array)$arr_model as $k=>$v){
			$this->view->$k=(array)$v;
		}

		$this->view->total_score=$total_score;
		//echo '<pre>';print_r($arr_model);exit;
	}
	
	function eduAction(){

		$this->userAction();
		
		$this->_helper->viewRenderer->render ( 'user' );
	}



	function educontentAction(){
		//loaded user info
		$params=$this->getRequest()->getParams();

		

		if(isset($params['user_id'])){
			$user_id=(int)$params['user_id'];
		}else{
			$user_id=(int)$_SESSION['admin']['user']['user_id'];
		}
		if(isset($params['begin_time'])){
			$begin_time=(int)$params['begin_time'];
		}else{
			$begin_time=(int)date("Y");
		}
		if(isset($params['model_id'])){
			$model_id=(int)$params["model_id"];
		}

		$User=new User();
		$rs_user=$User->getOne($user_id);		
		$this->view->user=@$rs_user[0];
		
		//loaded model
		
		if(!$model_id){
			throw new Exception(BAOGG_LANGUAGE::get('hack'));
		}
		$ModelTable=new ModelTable($model_id);
		$rs_list=$ModelTable->getShowList(array("user_id"=>(int)$user_id,"begin_time"=>$begin_time."-%"));
		$this->view->edu_list=$rs_list;
		$quarter_score=array(0,0,0,0);
		$year_score=0;
		foreach((array)$rs_list as $v){
			$year_score+=$v['score'];
			//convert month to quarter
			$quarter= Baogg_Date::month2quarter(substr($v['begin_time'],6,2));
			$quarter_score[$quarter]+=$v['score'];
		}
		$this->view->quarter_score=$quarter_score;
		$this->view->year_score=$year_score;
		$this->view->begin_time=$begin_time;
		$rs_list=$ModelTable->getShowList(array("user_id"=>(int)$user_id,"begin_time"=>$begin_time."-%"));
		$this->view->list=$rs_list;
		$this->view->quarter_list=array();
		$this->view->year_list=array();

		//echo '<pre>';print_r($rs_user[0]);exit;
	}
	
	function allAction(){
		$this->userAction();
	}
	function allcontentAction(){
		
		UserInfo::updateScore();
		
		$begin_time=$this->getRequest()->getParam("begin_time");
		
		//get top user group		
		$UserGroup=new UserGroup();
		$rs_parent=$UserGroup->getArray(array("user_group_pid"=>0,"state"=>1));
		$arr_parent=array_keys($rs_parent);
		$arr_group=array();
		$rs_group=$UserGroup->getList(array("user_group_pid"=>$arr_parent,"state"=>1),array("sort"=>"user_group_pid","dir"=>"asc"),null,array("user_group_id","user_group_name"));
		foreach((array)$rs_group as $v){
			$arr_group[$v['user_group_id']]=$v['user_group_name'];
		}
		unset($rs_group);
		
		
		$User=new User();
		$arr_user_name=array();
		
		$Model=new Model();
		

		$arr_quarter=array();  //quanter static
		$arr_total=array(); //total static
		$total_score=0;
		$arr_user=array(array(),array(),array(),array()); 
		$arr_user_num=array(array(),array(),array(),array());//user number per quarter,for table rowspan
		
		$rs_model=$Model->getList(array("state"=>1,"main_table"=>"user"));	
		foreach((array)$rs_model as $schema){
			
			$arr_total[$schema['ext_table']]=0;			
			
			$ModelTable=new ModelTable((int)$schema['model_id']);
			$rs_model_table=$ModelTable->getShowList(array("begin_time"=>$begin_time."-%"));
			foreach((array)$rs_model_table as $v){
				$rs_user=$User->getOne($v['user_id']);				
				//not exist user
				if(!isset($rs_user[0])){
					continue;
				}else{
					$arr_user_name[$rs_user[0]['user_id']]=$rs_user[0]['user_name'];
				}
				$user_group_pid=(int)$UserGroup->getTopGroupById($rs_user[0]['user_group_id']);
				
				$quarter= Baogg_Date::month2quarter(substr($v['begin_time'],5,2));
				
				@$arr_user[$quarter][$user_group_pid][$v['user_id']][$schema['ext_table']]+=$v['score'];
				@$arr_user[$quarter][$user_group_pid][$v['user_id']]['total']+=$v['score'];
				@$arr_user_num[$quarter][$v['user_id']]=1;
				
				@$arr_quarter[$quarter][$schema['ext_table']]+=$v['score'];
				@$arr_quarter[$quarter]['total']+=$v['score'];
				
				@$arr_total[$schema['ext_table']]+=$v['score'];				
				@$arr_total['total']+=$v['score'];
				
				/*if($schema['ext_table']=='user_report'){
					echo '<pre>';
					print_r($v); 
					echo $user_group_pid;
					var_dump($quarter);
					echo substr($v['begin_time'],5,2);
					exit;
				}*/
			}
		}
		
		$this->view->error_reporting = 0;
		$this->view->users=$arr_user;
		$this->view->user_num=$arr_user_num;
		$this->view->quarters=$arr_quarter;
		$this->view->totals=$arr_total;
		$this->view->quarter_name=array("一","二","三","四");
		$this->view->year=date("Y",strtotime($begin_time));
		$this->view->groups=$arr_group;
		$this->view->user_name=$arr_user_name;
		
		/*echo '<pre>';
		print_r($arr_user);
		echo Baogg_Db::debugDb(Zend_Registry::get("db"));
		exit;*/
	}
}
?>
